package com.gzbugu.bugusys.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.bean.PageModel;
import com.gzbugu.bugusys.dao.IHQLQuery;

@Service
@Transactional
public class ShowContractServiceService {

	private PageModel pageModel;
	@Resource(name = "hqlQuery")
	private IHQLQuery hqlQuery;

	public PageModel getPageModel() {
		if (this.pageModel == null) {
			return new PageModel();
		}
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public List findContractServiceByTypeId(Short typeId) {
		String query = "from ContractService as model where model.type.id = "
				+ typeId;
		this.getPageModel().setLineCount((hqlQuery.find(query).size()));
		return hqlQuery.find(query, this.getPageModel());
	}

	public List findAllContractService() {
		String query = "from ContractService";
		this.getPageModel().setLineCount((hqlQuery.find(query).size()));
		return hqlQuery.find(query, this.getPageModel());
	}

	public List findContractServiceByMonth(Integer year, Integer month,
			Short contractServiceTypeId) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		String query = "from ContractService as model where model.start >=:start and model.start <=:end "
				+ (contractServiceTypeId == null ? ""
						: " and model.type.id =:typeId");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("start", start);
		paramsMap.put("end", end);
		if (contractServiceTypeId != null)
			paramsMap.put("typeId", contractServiceTypeId);
		this.getPageModel().setLineCount(
				(hqlQuery.find(query, paramsMap).size()));
		return hqlQuery.find(query, paramsMap, pageModel);
	}

	public List findContractServiceByMonthAndId(String staffId, Integer year,
			Integer month, Short contractServiceTypeId) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);
		Date start = calendar.getTime();// 开始日期为指定月份第一天
		calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
		Date end = calendar.getTime();// 结束日期为指定月份的最后一天
		String query = "from ContractService as model where model.staff.staffId =:staffId and model.start >=:start and model.start <=:end "
				+ (contractServiceTypeId == null ? ""
						: " and model.type.id =:typeId");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("start", start);
		paramsMap.put("end", end);
		paramsMap.put("staffId", staffId);
		if (contractServiceTypeId != null)
			paramsMap.put("typeId", contractServiceTypeId);
		this.getPageModel().setLineCount(
				(hqlQuery.find(query, paramsMap).size()));
		return hqlQuery.find(query, paramsMap, pageModel);
	}

	@SuppressWarnings("unchecked")
	public List findContractServiceByStaffId(String staffId,
			Short contractServiceTypeId) {
		String query = "from ContractService as model where model.staff.staffId =:staffId"
				+ (contractServiceTypeId == null ? ""
						: " and model.type.id =:typeId");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("staffId", staffId);
		if (contractServiceTypeId != null)
			paramsMap.put("typeId", contractServiceTypeId);
		this.getPageModel().setLineCount(
				(hqlQuery.find(query, paramsMap).size()));
		return hqlQuery.find(query, paramsMap, pageModel);
	}
}